package com.ruicar.afs.cloud.seats.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tiankai
 * @Describe 日期获取
 * Created on 2020-05-15
 */
public class DateUtil {

    /**
     * 默认日期格式
     */
    public static String DEFAULT_FORMAT = "yyyy-MM-dd";

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatDate(Date date) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 获取当年的第一天
     *
     * @return
     */
    public static Date getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     *
     * @return
     */
    public static Date getCurrYearLast() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

    /**
     * 获取当前是星期几
     *
     * @param calendar
     * @return String
     */
    public static String getCurrentWeekOfMonth(Calendar calendar) {
        String strWeek = "";
        int dw = calendar.get(Calendar.DAY_OF_WEEK);
        if (dw == 1) {
            strWeek = "星期日";
        } else if (dw == 2) {
            strWeek = "星期一";
        } else if (dw == 3) {
            strWeek = "星期二";
        } else if (dw == 4) {
            strWeek = "星期三";
        } else if (dw == 5) {
            strWeek = "星期四";
        } else if (dw == 6) {
            strWeek = "星期五";
        } else if (dw == 7) {
            strWeek = "星期六";
        }
        return strWeek;
    }
}
